<template>
  <div class="pushBox">
    <el-dialog :close-on-click-modal='false' title="发布作品" :visible.sync="dialogVisible" width="50%"
      :before-close="handleClose">
      <div class="contentox" v-if="form">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="作品图片" width='200' prop='img'>
            <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false" :on-success="handleAvatarSuccess">
              <img v-if="form.img" :src="form.img" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="作品名称" width="80px" prop='text'>
            <el-input v-model="form.text"></el-input>
          </el-form-item>
          <el-form-item label="设备类型" prop="phone">
            <el-select v-model="form.phone" placeholder="请选择拍摄的设备">
              <el-option label="DJI Mavic 3" value="DJI Mavic 3"></el-option>
              <el-option label="DJI Air 2S" value="DJI Air 2S"></el-option>
              <el-option label="Osmo Pocket" value="Osmo Pocket"></el-option>
              <el-option label="Mavic Air 2" value="Mavic Air 2"></el-option>
              <el-option label="Mavic 2 Pro" value="Mavic 2 Pro"></el-option>
              <el-option label="DJI Mini 2" value="DJI Mini 2"></el-option>
              <el-option label="Ohter" value="Ohter"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose()">取 消</el-button>
        <el-button type="info" @click="resetForm('form')">重 置</el-button>
        <el-button type="primary" @click="confrimFun('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {
    dialogVisible: Boolean,
  },
  data() {
    return {
      form: {
        img: null,
        text: null,
        phone: null,
      },
      rules: {
        text: [
          { required: true, message: '请输入作品名称', trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '请选择拍摄设备类型', trigger: 'change' }
        ],
        img: [
          { required: true, message: '请上传图片', trigger: 'change' }
        ],
      }
    };
  },
  methods: {
    handleClose() {
      this.$emit('close');
      this.resetForm('form');
    },
    handleAvatarSuccess(res, file) {
      this.form.img = URL.createObjectURL(file.raw);
    },
    confrimFun(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let userInfo = this.$store.state.userInfo;
          let params = {
            img: this.form.img,
            text: this.form.text,
            phone: this.form.phone,
            userimg: userInfo.headpic,
            username: userInfo.username,
            userid: userInfo.id,
            time: new Date().getTime(),
          }

          this.$axios.post('/api/pushOpus', params).then((res) => {
            let { status } = res
            if (status == 200) {
              this.$message({
                message: '发布成功',
                type: 'success'
              });
              this.resetForm('form');
              this.$emit('updata')
            }
          }).finally(() => {
            this.handleClose();
          })
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.form.img = null;
      this.form.text = null;
      this.form.phone = null;
    }
  },
}
</script>
<style scoped lang='scss'>
.avatar-uploader {
  width: 200px;
  height: 200px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 200px;
  height: 200px;
  display: block;
}
.contentox {
  padding: 0 20px;
}
.pushBox {
  &::v-deep .el-input--suffix {
    width: 596px;
  }
}
</style>